// 建立全局 websocket
export function createGlobalWebsocket(userId) {
    const globalSocket = new WebSocket(`ws://82.156.124.246:9900/websocket/global/${userId}`);

    globalSocket.onopen = () => {
        console.log('Global Websocket Connect!');
    };

    globalSocket.onclose = () => {
        console.log('Global Websocket closed!')
    };

    globalSocket.onerror = (error) => {
        console.log("Websocket Connection Error: ", error);
    };

    return globalSocket;
};

// 指定对象通信
export function createdChatWebsocket(sellerInfo, customerId) {
    const token = JSON.parse(localStorage.getItem('token'));

    const socket = new WebSocket(`ws://82.156.124.246:9900/websocket/group?customerId=${customerId}&sellerId=${sellerInfo.id}&token=${token}`);

    socket.onopen = () => {
        console.log(`Chat with ${sellerInfo.name} open!`);
    };

    socket.onclose = () => {
        console.log(`Chat with ${sellerInfo.name} closed!`)
    };

    socket.onerror = (error) => {
        console.log("Websocket Connection Error: ", error);
    };

    return socket;
};